<template>
  <div class="header">
    <div class="title">
      <div class="titleOne">{{ title }}</div>
      <div @click="jumpComponent(titleAdd)" class="titleTwo" v-if="titleAdd">{{ titleAdd }}</div>
    </div>
    <div class="headerImg">
      <img @click="turnDown" src="@/assets/headerImg/down.png" class="imgOne">
      <img @click="close" src="@/assets/headerImg/x.png">
    </div>
  </div>
</template>

<script>
import {mapMutations} from "vuex";

export default {
  name: "Header",
  data() {
    return {
      turn: false
    }
  },
  props: ['title', 'titleAdd'],
  methods: {
    ...mapMutations('roles', ['showRoleAddDialog']),
    ...mapMutations('admin', ['showAdminAddDialog']),
    turnDown(e) {
      let allBro = e.target.parentNode.parentNode.parentNode.childNodes;
      // console.log(allBro);
      if (!this.turn) {
        e.target.style.transform = 'rotate(180deg)'
        this.turn = true
        for (let i = 0; i < allBro.length; i++) {
          if (i !== 0) {
            allBro[i].style.opacity = '0'
          }
        }
      } else {
        let allBro = e.target.parentNode.parentNode.parentNode.childNodes;
        e.target.style.transform = 'rotate(0deg)'
        this.turn = false
        for (let i = 0; i < allBro.length; i++) {
          if (i !== 0) {
            allBro[i].style.opacity = '1'
          }
        }
      }
    },
    close(e) {
      e.target.parentNode.parentNode.parentNode.style.opacity = '0'
    },
    jumpComponent(titleAdd) {
      if (titleAdd === '添加管理员') {
        this.showAdminAddDialog()
      } else if (titleAdd === '添加角色') {
        this.showRoleAddDialog()
      }
    }
  }
}
</script>

<style scoped>
.header {
  width: 96%;
  margin: auto;
  height: 50px;
  border-bottom: 1px solid lightgray;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.title {
  width: 240px;
  height: 50px;
}

.headerImg {
  padding-top: 15px;
  width: 70px;
  height: 50px;
}

img {
  margin-left: 15px;
  width: 20px;
  height: 20px;
}

.imgOne {
  transition: all linear 0.3s;
}

.titleOne {
  float: left;
  width: 120px;
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  color: gray;
}

.titleTwo {
  float: left;
  width: 120px;
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  color: rgb(29, 132, 198);
  cursor: pointer;
}
</style>